package com.sino.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sino.operation.commons.commonEnum.TellerState;
import com.sino.operation.entity.SysTeller;
import com.sino.operation.mapper.SysTellerMapper;
import com.sino.operation.service.SysTellerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中心柜员表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-07
 */
@Service
public class SysTellerServiceImpl extends ServiceImpl<SysTellerMapper, SysTeller> implements SysTellerService {

    @Autowired(required = false)
    private SysTellerMapper sysTellerMapper;

    @Override
    public IPage<SysTeller> selectList(Map<String, String> map) {
        QueryWrapper<SysTeller> wrapper = new QueryWrapper<>();
        String TELLER_POST = map.get("TELLER_POST");//柜员岗位
        String TELLER_STATE = map.get("TELLER_STATE");//签到状态
        String TELLER_CODE = map.get("TELLER_CODE");//柜员号
        String CENTER_NO = map.get("CENTER_NO");//中心号
        if (!TELLER_POST.isEmpty()) {
            wrapper.eq("TELLER_POST", TELLER_POST);
        }
        if (!TELLER_STATE.isEmpty()) {
            wrapper.eq("TELLER_STATE", TELLER_STATE);
        }
        if (!TELLER_CODE.isEmpty()) {
            wrapper.eq("TELLER_CODE", TELLER_CODE);
        }
        if (!CENTER_NO.isEmpty()) {
            wrapper.eq("CENTER_NO", CENTER_NO);
        }
        int current = Integer.parseInt(map.get("current"));
        int size = Integer.parseInt(map.get("size"));
        return sysTellerMapper.selectPage(new Page<>(current, size), wrapper);
    }

    @Override
    public IPage<SysTeller> selectOutsideList(Map<String, String> map) {
        QueryWrapper<SysTeller> wrapper = new QueryWrapper<>();
        if (map.containsKey("centerNo") && map.get("centerNo") != null && !map.get("centerNo").trim().isEmpty()) {
            wrapper.eq("CENTER_NO", map.get("centerNo"));
        }
        if (map.containsKey("tellerCode") && map.get("tellerCode") != null && !map.get("tellerCode").trim().isEmpty()) {
            wrapper.eq("TELLER_CODE", map.get("tellerCode"));
        }
        if (map.containsKey("tellerPost") && map.get("tellerPost") != null && !map.get("tellerPost").trim().isEmpty()) {
            wrapper.eq("TELLER_POST", map.get("tellerPost"));
        }
        if (map.containsKey("TELLER_STATE") && map.get("tellerState") != null && !map.get("tellerState").trim().isEmpty()) {
            wrapper.eq("TELLER_CODE", map.get("tellerState"));
        }
        return sysTellerMapper.selectPage(new Page<>(Integer.parseInt(map.get("current")), Integer.parseInt(map.get("size"))), wrapper);
    }

    @Override
    public boolean resetPassword(List<String> ids) {
        Collection<SysTeller> needResetData = listByIds(ids);
        needResetData.forEach(item -> {
            //重置密码，可以考虑配成配置文件
            item.setTellerPassword("123456");
        });
        return updateBatchById(needResetData);
    }

    @Override
    public boolean signOut(List<String> ids) {
        Collection<SysTeller> needResetData = listByIds(ids);
        needResetData.forEach(item -> {
            item.setTellerState(TellerState.TellerState2.getValue());
        });
        return updateBatchById(needResetData);
    }

    @Override
    public SysTeller selectTellerByTellerCode(String tellerCode) {
        return sysTellerMapper.selectTellerByTellerCode(tellerCode);
    }

    @Override
    public List<String> getTransTypeByTellerCode(String tellerCode) {
        return sysTellerMapper.getTransTypeByTellerCode(tellerCode);
    }

}
